﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;
namespace DAO
{
    public class DAO_QTTH
    {
        public static List<DTO_QTTH> Select_QTTH()
        {
            List<DTO_QTTH> listqtth = new List<DTO_QTTH>();
            try
            {
                string s = "Proc_Select_QTTH";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_QTTH qtth = new DTO_QTTH();
                    qtth.ID_tuan = int.Parse(dt.Rows[i]["ID_tuan"].ToString());
                    qtth.ID_PCDT = int.Parse(dt.Rows[i]["ID_PCDT"].ToString());
                    qtth.ND_Yeucau = dt.Rows[i]["ND_Yeucau"].ToString();
                    qtth.NgayYeuCau = DateTime.Parse(dt.Rows[i]["NgayYeuCau"].ToString());
                    qtth.ND_Baocao = dt.Rows[i]["ND_Baocao"].ToString();
                    qtth.NgayBaoCao = DateTime.Parse(dt.Rows[i]["NgayBaoCao"].ToString());
                    listqtth.Add(qtth);
                }
                ketnoi.Cmb().Close();
                return listqtth;
            }
            catch (Exception ex)
            {
                return listqtth;
            }
        }

        public static List<DTO_QTTH> Select_QTTH_ID(int id_pc)
        {
            List<DTO_QTTH> listidth = new List<DTO_QTTH>();
            try
            {
                string s = "Proc_Select_QTTH_IDPCDT";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@ID_PCDT", SqlDbType.NVarChar).Value = id_pc;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_QTTH qtth = new DTO_QTTH();
                    qtth.ID_PCDT = int.Parse(dt.Rows[i]["ID_PCDT"].ToString());
                    qtth.ID_tuan = int.Parse(dt.Rows[i]["ID_tuan"].ToString());
                    qtth.ID_PCDT = int.Parse(dt.Rows[i]["ID_PCDT"].ToString());
                    qtth.ND_Yeucau = dt.Rows[i]["ND_Yeucau"].ToString();
                    qtth.NgayYeuCau = DateTime.Parse(dt.Rows[i]["NgayYeuCau"].ToString());
                    qtth.ND_Baocao = dt.Rows[i]["ND_Baocao"].ToString();
                    qtth.NgayBaoCao = DateTime.Parse(dt.Rows[i]["NgayBaoCao"].ToString());
                    listidth.Add(qtth);
                }
                ketnoi.Cmb().Close();
                return listidth;
            }
            catch (Exception ex)
            {
                return listidth;
            }
        }

        public static bool UpdateQTTH(DTO_QTTH qt, int id_tuan, int id_pcdt)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_UpdateQTTH";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@ND_Yeucau", qt.ND_Yeucau);
                cmd1.Parameters.AddWithValue("@ND_Baocao", qt.ND_Baocao);
                cmd1.Parameters.AddWithValue("@NgayYeuCau", qt.NgayYeuCau);
                cmd1.Parameters.AddWithValue("@NgayBaoCao", qt.NgayBaoCao);
                cmd1.Parameters.AddWithValue("@ID_tuan", id_tuan);
                cmd1.Parameters.AddWithValue("@ID_PCDT", id_pcdt);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool insertQTTH(int id,int tuan)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "proc_InsetQTTH_new";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@ID_PCDT", id);
                cmd1.Parameters.AddWithValue("@ID_Tuan", tuan);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool deleteQTTH(string ID_tuan)
        {
            List<DTO_QTTH> listQTTH = new List<DTO_QTTH>();

            try
            {
                string s = "Proc_deleteQTTH";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@ID_tuan", SqlDbType.NVarChar).Value = ID_tuan;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable sv = new DataTable();
                da.Fill(sv);
                for (int i = 0; i < sv.Rows.Count; i++)
                {
                    DTO_QTTH qtth = new DTO_QTTH();
                    qtth.ID_tuan = int.Parse(sv.Rows[i]["ID_tuan"].ToString());
                    listQTTH.Add(qtth);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}